<template>
  <!--搜索-->
  <el-form class="query-container" ref="queryFormRef" :model="queryParams" :inline="true" @submit.native.prevent>
    <el-form-item label="姓名" prop="name">
      <el-input
          v-model="queryParams.name"
          clearable
          placeholder="请输入姓名"
          @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="证件类型" prop="authenticationType">
      <el-input
          v-model="queryParams.authenticationType"
          clearable
          placeholder="证件类型"
          @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="证件号码" prop="authenticationNumber">
      <el-input
          v-model="queryParams.authenticationNumber"
          clearable
          placeholder="证件号码"
          @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleQuery">搜索</el-button>
      <el-button @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>

  <div class="inner-container">
    <el-row :gutter="10" style="margin-bottom:8px">
      <el-col :span="1.5">
        <el-button type="success" plain @click="showEditDialog" :disabled="selectedIds.length!==1">审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain @click="showEditDialog" :disabled="selectedIds.length!==1">反审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain @click="handleDelete(null)" :disabled="!selectedIds.length">删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="30"/>
      <el-table-column prop="id" label="证件表id" width="160px"/>
      <el-table-column prop="authenticationType" label="证件类型" :formatter="authenticationTypeFormatter" show-overflow-tooltip/>
<!--      <el-table-column prop="userId" label="用户ID" show-overflow-tooltip/>-->
      <el-table-column prop="name" label="名字" show-overflow-tooltip/>
      <el-table-column prop="authenticationNumber" label="证件号码" show-overflow-tooltip/>
      <el-table-column prop="nationality" label="国籍" :formatter="nationalityFormatter" show-overflow-tooltip/>
      <el-table-column prop="validDateStart" label="时效起" show-overflow-tooltip/>
      <el-table-column prop="validDateEnd" label="时效末" show-overflow-tooltip/>
      <el-table-column prop="status" label="证件状态" :formatter="statusFormatter" show-overflow-tooltip width="100px"/>
      <el-table-column prop="auditing" label="审核状态" :formatter="auditStatusFormatter" show-overflow-tooltip width="100px"/>
      <el-table-column prop="applicationId" label="审核申请id" show-overflow-tooltip  width="100px"/>
      <el-table-column prop="createTime" label="创建时间" width="160px" show-overflow-tooltip/>
      <el-table-column prop="selection" label="选中" :formatter="selectionStatusFormatter" show-overflow-tooltip width="100px"/>

      <el-table-column label="操作" fixed="right" width="150">
        <template #default="scope">
          <el-button type="primary" link icon="Edit" @click="showEditDialog(scope.row)">审核</el-button>
          <el-button type="primary" link icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>
    <!--Element UI 提供的分页组件-->
    <el-pagination
        :total="total"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    <!--新增、修改订单弹窗-->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="closeDialog" append-to-body>
      <el-form ref="dialogFormRef" :model="formData" :rules="formRules" label-width="100px">
        <el-form-item label="证件表ID" prop="id" style="width:50%;" >
          <el-input readonly v-model="formData.id" placeholder="无需填写，将由系统生成"/>
        </el-form-item>
        <el-form-item label="证件号码" prop="authenticationNumber" style="width:50%;" >
          <el-input readonly v-model="formData.authenticationNumber" placeholder="证件号码"/>
        </el-form-item>
        <el-form-item label="姓名" prop="name" style="width:50%;">
          <el-input readonly v-model="formData.name" placeholder="请输入用户姓名"/>
        </el-form-item>

        <el-form-item label="审核状态"  prop="auditingStatus" style="width:30%;">
          <el-select v-model="formData.auditing" placeholder="审核状态" style="width:96%;">
            <el-option value="0" label="通过"></el-option>
            <el-option value="1" label="审核中"></el-option>
            <el-option value="2" label="拒绝"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer class="dialog-footer">
        <el-button type="primary" @click="saveOrUpdate">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </template>
    </el-dialog>
    <!--弹窗结束-->
  </div>

</template>

<script setup>
import {onMounted, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import request from '@/utils/request'

// --------------------------------------------------

const loading = ref(false)
const queryFormRef = ref()
const queryParams = ref({pageNum: 1, pageSize: 10})
const total = ref(0)
const tableData = ref([])
const selectedIds = ref([])


const listData = () => {
  loading.value = true
  request.get('/api/bus/users/authentication', {
    params: queryParams.value
  }).then(res => {
    total.value = res.data.total
    tableData.value = res.data.list
    loading.value = false
  })
}

const handleSelectionChange = (selection) => {
  selectedIds.value = selection.map(item => item.id)
}

const handleQuery = () => {
  queryParams.value.pageNum = 1
  listData()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  listData()
}

const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  listData()
}


const handleDelete = (row) => {
  let ids = row ? [row.id] : [...selectedIds.value]
  if (ids.length === 0) {
    ElMessage.error('请选择要删除的记录')
    return
  }
  ElMessageBox.confirm('确定删除吗？', '提示', {
    type: 'warning'
  }).then(() => {
    request.delete(`/api/bus/users/authentication?ids=${ids}`)
        .then(res => {
          selectedIds.value.length = 0
          listData()
          ElMessage.success('删除成功')
        })
  }).catch(() => {})
}
// --------------------------------------------------

//定义变量用来保存起止时间
const times = ref([]);



const dialogTitle = ref('')
const dialogVisible = ref(false)
const dialogFormRef = ref()
const formData = ref({
  id: undefined,
  name: undefined,
  userId: undefined,
  authenticationType: undefined,
  authenticationNumber: undefined,
  nationality: undefined,
  validDateStart: undefined,
  validDateEnd: undefined,
  status: undefined,
  auditing: undefined,
  selection: undefined,
  createTime: undefined

})

const formRules = ref({

})
const showAddDialog = () => {
  dialogTitle.value = '新增认证'
  dialogVisible.value = true
}
//
const showEditDialog = (row) => {
  let id = row.id || selectedIds.value
  request.get(`/api/bus/users/authentication/${id}`)
      .then(res => {
        formData.value = res.data
        formData.value.auditing = formData.value.auditing.toString()
        dialogTitle.value = '审核认证信息'
        dialogVisible.value = true
      })
}
const closeDialog = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    userId: undefined,
    authenticationType: undefined,
    authenticationNumber: undefined,
    nationality: undefined,
    validDateStart: undefined,
    validDateEnd: undefined,
    status: undefined,
    auditing: undefined,
    selection: undefined,
    createTime: undefined

  }
  dialogFormRef.value.resetFields()
  dialogVisible.value = false
}

const saveOrUpdate = () => {
  dialogFormRef.value.validate(valid => {

    const id = formData.value.id;
    console.log(formData.value)
    // console.log(formData.value)
    request({
      url: '/api/bus/users/authentication',
      method: id ? 'put' : 'post',
      data: formData.value
    }).then(res => {
      closeDialog();
      listData();
      ElMessage.success(id ? '修改成功' : '新增成功');
    });
  });
};

//加载数据
onMounted(() => {
  listData()
})



const authenticationTypeFormatter = (row,column,cellValue,index)=>{
  if (cellValue==84){
    cellValue = '身份证';
  }else if (cellValue==85){
    cellValue = '护照';
  }else if (cellValue==86){
    cellValue = '台胞证';
  }else if (cellValue==87){
    cellValue = '港澳居民来往内地通行证';
  }else if (cellValue==88){
    cellValue = '港澳居民证';
  }else if (cellValue==89){
    cellValue = '外国人永久居留证';
  }
  return cellValue;
}
// 84,16,身份证
// 85,16,护照
// 86,16,台胞证
// 87,16,港澳居民来往内地通行证
// 88,16,港澳居民证
// 89,16,外国人永久居留证





const nationalityFormatter = (row,column,cellValue,index)=>{
  if (cellValue==90){
    cellValue = '中国';
  }else if (cellValue==91){
    cellValue = '中国香港特别行政区';
  }else if (cellValue==92){
    cellValue = '中国香港特别行政区';
  }else if (cellValue==93){
    cellValue = '中国台湾';
  }else if (cellValue==94){
    cellValue = '美国';
  }else if (cellValue==95){
    cellValue = '英国';
  }else if (cellValue==96){
    cellValue = '澳大利亚';
  }
  return cellValue;
}

// 90,17,中国
// 91,17,中国香港特别行政区
// 92,17,中国澳门特别行政区
// 93,17,中国台湾
// 94,17,美国
// 95,17,英国
// 96,17,澳大利亚



const statusFormatter = (row,column,cellValue,index)=>{
  if (cellValue==0){
    cellValue = '有效';
  }else if (cellValue==1){
    cellValue = '过期';
  }
  return cellValue;
}

const auditStatusFormatter = (row,column,cellValue,index)=>{
  if (cellValue==0){
    cellValue = '通过';
  }else if (cellValue==1){
    cellValue = '审核中';
  }else if (cellValue==2){
    cellValue = '拒绝';
  }
  return cellValue;
}


const selectionStatusFormatter = (row,column,cellValue,index)=>{
  if (cellValue==0){
    cellValue = '选中';
  }else if (cellValue==1){
    cellValue = '未选中';
  }
  return cellValue;
}

</script>

<style scoped>

</style>
